package cn.xuchunh.guava.base;

import com.google.common.base.Objects;
import com.google.common.collect.ComparisonChain;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCH on 2017/3/29.
 * <p>
 * <a href="http://wiki.jikexueyuan.com/project/google-guava-official-tutorial/common-object-ways.html">常见Object方法</a>
 *
 * @author XCH
 * @version 1.0.0-SNAPSHOT
 */
public class EasyObject {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static void main(String[] args) {
        // equals
        System.out.println(Objects.equal(null, "a"));
        System.out.println(Objects.equal("a", "a"));
        System.out.println(Objects.equal("b", "a"));
        System.out.println(Objects.equal(null, null));

        // hashcode
        // Guava 的 Objects.hashCode(Object...)会对传入的字段序列计算出合理的、顺序敏感的散列值。
        System.out.println(Objects.hashCode("fffff"));
        System.out.println("fffff".hashCode());

        String s = "fffff";
        System.out.println(Objects.hashCode(s));
        System.out.println(s.hashCode());

        // toString
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i + "");
        }
        // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        System.out.println(list.toString());
        // ArrayList{name=xuchunh}
        System.out.println(Objects.toStringHelper(list).add("name", "xuchunh"));

        // compare
        Actor king = new Actor("xuchunh", 23);
        Actor queen = new Actor("xuchunh", 45);
        int result = ComparisonChain.start()
                .compare(king.getName(), queen.getName())
                .compare(king.getAge(), queen.getAge())
                .result();
        System.out.println(result);

    }

}
